import {
  Body,
  Controller,
  Delete,
  Get,
  Param,
  Patch,
  Post,
  Query,
  Req,
  Session,
  UseGuards,
  UseInterceptors,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth, ApiHeader, ApiOkResponse, ApiOperation, ApiTags } from '@nestjs/swagger';

import { LoggerInterceptor } from '@/interceptor/logger.interceptor';
import {
  DeleteDto,
  ResponseFileCollectionOneDto,
} from '@/modules/administrative/file-collection/dto/response.file_collection.dto';
import { Logger } from '@/utils/log4js';
import type { SessionTypes } from '@/utils/types';

import {
  CreateFileCollectionDto,
  ListFileCollectionDto,
  ResponseFileCollectionDto,
  UpdateFileCollectionDto,
} from './dto/index';
import { FileCollectionService } from './file_collection.service';

@ApiTags('智能行政-文件收集')
@ApiHeader({
  name: 'Authorization',
  description: 'token令牌',
  required: true,
})
@ApiBearerAuth()
@UseInterceptors(LoggerInterceptor)
@UseGuards(AuthGuard('jwt'))
@Controller('administrative/file-collection')
export class FileCollectionController {
  constructor(private readonly fileCollectionService: FileCollectionService) {}

  @Post()
  @ApiOkResponse({ type: CreateFileCollectionDto })
  @ApiOperation({ summary: '创建文件收集任务' })
  async create(
    @Body() createFileCollectionDto: CreateFileCollectionDto,
    @Session() session: SessionTypes,
    @Req() req: Request,
  ) {
    return await this.fileCollectionService.create(
      createFileCollectionDto,
      session,
      req.headers['authorization'],
    );
  }

  /**
   * @description: 获取所有文件收集任务
   * @author YoungYa
   */
  @Get()
  @ApiOkResponse({ type: ResponseFileCollectionDto })
  @ApiOperation({ summary: '获取文件收集任务列表' })
  findAll(@Query() fileCollectionList: ListFileCollectionDto, @Req() req: Request) {
    return this.fileCollectionService.findAll(fileCollectionList, req.headers['authorization']);
  }

  @Get('/all')
  @ApiOkResponse({ type: ResponseFileCollectionDto })
  @ApiOperation({ summary: '提交者获取全部文件收集任务列表' })
  async findAllTask(@Query() fileCollectionList: ListFileCollectionDto) {
    return await this.fileCollectionService.findAllTask(fileCollectionList);
  }

  @Get(':id')
  @ApiOkResponse({ type: ResponseFileCollectionOneDto })
  @ApiOperation({ summary: '获取文件收集任务详情' })
  findOne(@Param('id') id: string) {
    return this.fileCollectionService.findOne(id);
  }

  @Patch(':id')
  update(@Param('id') id: string, @Body() updateFileCollectionDto: UpdateFileCollectionDto) {
    return this.fileCollectionService.update(+id, updateFileCollectionDto);
  }

  @Delete()
  @ApiOkResponse({ type: ResponseFileCollectionDto })
  @ApiOperation({ summary: '删除文件收集任务' })
  async remove(@Body() body: DeleteDto) {
    Logger.debug('删除文件收集任务', body);
    const response = await this.fileCollectionService.remove(body.ids);
    return response;
  }
}
